        
using System;
using Dapper;
using System.Linq;
using Project.IData;
using Project.Model;
using System.Linq.Expressions;
using System.Collections.Generic;

namespace Project.Data 
{
    public partial class ActionInfoData : BaseData<ActionInfo>,IActionInfoData
    {
        /// <summary>
        /// 添加数据
        /// </summary>
        /// <param name="entity">插入数据对象</param>
        /// <returns>数据库插入条数</returns>
        public int Create(ActionInfo entity)
        {
			string sql=@"insert into ActionInfo(ActionName) values(:ActionName)";
            using (var connection = Database.GetConnection())
            {
                return connection.Query<int>(sql, entity).FirstOrDefault();
            }
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="entity">修改数据对象</param>
        /// <returns>修改成功条数</returns>
        public ActionInfo Edit(ActionInfo entity)
        {
            string sql = @"update ActionInfo set ActionName = :ActionName where ActionInfoId = :ActionInfoId";
            using (var connection = Database.GetConnection())
            {
                connection.Execute(sql, entity);
            }
            return entity;
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="entity">用户对象，只传Id即可</param>
        /// <returns>删除的行数</returns>
        public int Delete(ActionInfo entity)
        {
            string sql = "delete ActionInfo where ActionInfoId = :ActionInfoId ";
            using (var connection = Database.GetConnection())
            {
                return connection.Execute(sql, entity);
            }
        }
        /// <summary>
        /// 获取所有的数据
        /// </summary>
        /// <returns></returns>
        public IEnumerable<ActionInfo> LoadModel(Expression<Func<ActionInfo,bool>> lambdaWhere)
        {
            string sql = "select a.ActionInfoId,a.ActionName  from ActionInfo a"+ " where " + lambdaWhere.Body.ToString().Substring(1,lambdaWhere.Body.ToString().Length-2);

            using (var connection =Database.GetConnection())
            {
				 return  connection.Query<ActionInfo>(sql);
            }
        }
        /// <summary>
        /// 获取相应的分页数据
        /// </summary>
        /// <param name="pageIndex">第几页</param>
        /// <param name="pageCount">每页条数</param>
        /// <returns></returns>
        public IEnumerable<ActionInfo> LoadModelByPage(int pageIndex, int pageCount,out int total, Expression<Func<ActionInfo,bool>> lambdaWhere)
        {
var start = pageIndex * pageCount - pageCount;
            var end = pageIndex * pageCount;

            string sql = string.Format("select ActionInfoId, ActionName from (select row_number() over(order by u.UserInfoId desc) as rk, a.ActionInfoId,a.ActionName  from ActionInfo a) a where rk between {0} and {1}   and {2}",start,end, lambdaWhere.Body.ToString().Substring(1,lambdaWhere.Body.ToString().Length-2));

            using (var connection = Database.GetConnection())
            {
				var userList = connection.Query<ActionInfo>(sql);				
                
				total = userList.Count();
                return userList;
            }
        }

    }
}
